package com.xuecheng.learning.api;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.learning.model.dto.ChooseCourseDto;
import com.xuecheng.learning.model.dto.CourseTablesDto;
import com.xuecheng.learning.model.po.ChooseCourse;
import com.xuecheng.learning.service.ChooseCourseService;
import com.xuecheng.learning.service.CourseTablesService;
import com.xuecheng.learning.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("选课相关接口")
@Slf4j
@RestController
public class ChooseCourseController {
    @Autowired
    ChooseCourseService chooseCourseService;

    @Autowired
    CourseTablesService courseTablesService;

    @ApiOperation("选课")
    @PostMapping("/choosecourse/{courseId}")
    public ChooseCourseDto chooseCourse(@PathVariable Long courseId){
        //首先需要获取当前用户信息
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        if(user == null){
            XueChengPlusException.cast("请先登录后才能选课");
        }
        //然后根据用户id和课程id进行选课操作
        return chooseCourseService.chooseCourse(courseId,user.getId());
    }

    @ApiOperation("查询学习资格")
    @PostMapping("/choosecourse/learnstatus/{courseId}")
    public CourseTablesDto getLearnstatus(@PathVariable("courseId") Long courseId) {
        //登录用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        if(user == null){
            XueChengPlusException.cast("请登录后继续选课");
        }
        Long userId = user.getId();
        return  courseTablesService.getLearnStatus(userId, courseId);
    }
}
